/**
 * <b>项目名：</b>hclv-web<br/>
 * <b>包名：</b>com.flea.hclv.controller<br/>
 * <b>文件名：</b>HServiceOrderController.java<br/>
 * <b>版本信息：</b>1.0<br/>
 * <b>日期：</b>2018年11月16日 上午11:57:16<br/>
 * <b>COPYRIGHT 2010-2012 ALL RIGHTS RESERVED 虫子科技</b>-版权所有<br/>
 */
package com.flea.hclv.controller;

import com.flea.hclv.constant.HConstant;
import com.flea.hclv.model.entity.HServiceOrder;
import com.flea.hclv.model.entity.HServiceOrderUpdateApply;
import com.flea.hclv.model.entity.HServicePayItem;
import com.flea.hclv.model.entity.HServicePayRebackItem;
import com.flea.hclv.model.entity.HTimeTable;
import com.flea.hclv.service.HServiceOrderService;
import com.flea.hclv.service.HServiceOrderUpdateApplyService;
import com.flea.hclv.service.HServicePayItemService;
import com.flea.hclv.service.HServicePayRebackItemService;
import com.flea.hclv.util.ServerResponse;
import com.flea.system.bean.FleaPage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @description 组团社与 地接，供应商之间的交易处理
 * @createTime 2018年11月16日 上午11:57:16
 * @modifyTime
 * @author myflea@163.com
 * @version 1.0
 */
@Controller
@RequestMapping("/flea/hclv/serviceOrder")
public class HServiceOrderController {

    private Logger logger = LoggerFactory.getLogger(HServiceOrderController.class);
    @Autowired
    private HServiceOrderService hServiceOrderService;
    @Autowired
    private HServiceOrderUpdateApplyService hServiceOrderUpdateApplyService;
    @Autowired
    private HServicePayItemService hServicePayItemService;
    @Autowired
    private HServicePayRebackItemService hServicePayRebackItemService;

    /**
     *
     * @description 创建服务组织结构之间的订单
     * @methodName addServiceOrder
     * @param
     * @throws Exception
     * @returnType ServerResponse<HServiceOrder>
     * @throw
     */
    @ResponseBody
    @RequestMapping("/addServiceOrder")
    public ServerResponse<HServiceOrder> addServiceOrder(
            HttpServletRequest request, @RequestParam("model") String model) {
        ServerResponse<HServiceOrder> result = null;
        logger.info("创建服务订单传入json：{}",model);
        try {
            result = hServiceOrderService.addOrder(request, model);
        } catch (Exception e) {
            logger.info("创建服务订单异常：",e.getMessage());
            return ServerResponse.createByErrorMessage(e.getMessage());

        }
        return result;
    }

    /**
     * TODO 业务描述： 1、是否允许更新 订单已经关闭，订单完成 这两种状态不允许修改
     *
     * 2、更新内容 3 更新记录插入的更新记录表中
     *
     * @description 更新订单信息
     * @methodName updateServiceOrder
     * @param
     * @returnType ServerResponse<HServiceOrder>
     * @throw
     */
    @ResponseBody
    @RequestMapping("/updateServiceOrder")
    public ServerResponse<HServiceOrder> updateServiceOrder(
            HttpServletRequest request, @RequestParam("model") String model) {
        ServerResponse<HServiceOrder> result = null;
        logger.info("传入将要更新的服务订单json:{}",model);
        try {
            result = hServiceOrderService.updateOrder(request, model);
        } catch (Exception e) {
            logger.info("更新服务订单异常:{}",e.getMessage());
            e.printStackTrace();
            return ServerResponse.createByErrorMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 确认订单
     * @param request
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateOrderToConfirm")
    public ServerResponse<HServiceOrder> updateOrderToConfirm(
            HttpServletRequest request, @RequestParam("model") String model) {
        ServerResponse<HServiceOrder> result = null;
        logger.info("确认服务订单传入json:{}",model);
        try {
            result = hServiceOrderService.updateOrderToConfirm(request, model);
        } catch (Exception e) {
            logger.info("确认服务订单异常",e.getMessage());
            e.printStackTrace();
            return ServerResponse.createByErrorMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 关闭订单
     * @param request
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateOrderToClose")
    public ServerResponse<HServiceOrder> updateOrderToClose(
            HttpServletRequest request, @RequestParam("model") String model) {
        ServerResponse<HServiceOrder> result = null;
        logger.info("关闭服务订单传入json:{}",model);
        try {
            result = hServiceOrderService.updateOrderToClose(request, model);
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByErrorMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 关闭订单
     * @param request
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateOrderToFinish")
    public ServerResponse<HServiceOrder> updateOrderToFinish(
            HttpServletRequest request, @RequestParam("model") String model) {
        logger.info("更新服务订单为完成，传入json:{}",model);
        ServerResponse<HServiceOrder> result = null;
        try {
            result = hServiceOrderService.updateOrderToFinish(request, model);
        } catch (Exception e) {
            logger.error("更新服务订单为完成异常",e.getMessage());
            e.printStackTrace();
            return ServerResponse.createByErrorMessage(e.getMessage());
        }
        return result;
    }


    /**
     * 销售商的订单列表
     * @description 获取当前部门的所有的订单信息，并且进行分页查询，可查询条件：订单号，订单状态
     * @methodName findAllOrderByOrgId
     * @param
     * @returnType ServerResponse<FleaPage < HServiceOrder>>
     * @throw
     */
    @ResponseBody
    @RequestMapping("/findAllOrgOrder")
    public ServerResponse<FleaPage<HServiceOrder>> findAllOrderByOrgIdForSale(
            HttpServletRequest request, FleaPage<HServiceOrder> paging, String model) {

        ServerResponse<FleaPage<HServiceOrder>> result = null;
        try {
            result = hServiceOrderService.findAllOrderByOrgIdPagingForSale(request, paging.getCurPage(),
                    paging.getPageSize(), model);
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByErrorMessage(e.getMessage());
        }
        return result;
    }
    /**
     * 购买商的订单列表
     * @description 获取当前部门的所有的订单信息，并且进行分页查询，可查询条件：订单号，订单状态
     * @methodName findAllOrderByOrgId
     * @param
     * @returnType ServerResponse<FleaPage < HServiceOrder>>
     * @throw
     */
    @ResponseBody
    @RequestMapping("/findAllOrgOrderForBuy")
    public ServerResponse<FleaPage<HServiceOrder>> findAllOrderByOrgIdForBuy(
            HttpServletRequest request, FleaPage<HServiceOrder> paging, String model) {

        ServerResponse<FleaPage<HServiceOrder>> result = null;
        try {
            result = hServiceOrderService.findAllOrderByOrgIdPagingForBuy(request, paging.getCurPage(),
                    paging.getPageSize(), model);
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByErrorMessage(e.getMessage());
        }
        return result;
    }

    /**
     *
     * @description 根据订单id，获取订单的详情
     * @methodName findDetailById
     * @param
     * @returnType ServerResponse<HServiceOrder>
     * @throw
     */
    @ResponseBody
    @RequestMapping("/findDetailById")
    public ServerResponse<Map<String, Object>> findDetailById(
            HttpServletRequest request,
            @RequestParam("model") String serviceOrderId) {

        ServerResponse<Map<String, Object>> result = hServiceOrderService.findByOrderId(serviceOrderId);

        return result;
    }

    /**
     *
     * @description 订单修改申请表
     * @methodName addOrderUpdateApply
     * @param
     * @returnType ServerResponse<HServiceOrderUpdateApply>
     * @throw
     */
    @ResponseBody
    @RequestMapping("/addServiceOrderUpdateApply")
    public ServerResponse<HServiceOrderUpdateApply> addOrderUpdateApply(
            HttpServletRequest request, @RequestParam("model") String model) {

        ServerResponse<HServiceOrderUpdateApply> result = null;
        try {
            result = hServiceOrderUpdateApplyService
                    .addUpdateApply(request, model);
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByErrorMessage(e.getMessage());
        }
        return result;
    }

    /**
     * model:1、申请的id
     *
     * @description 对修改申请的批复, 同意修改
     * @methodName updateApplyStatus
     * @param
     * @throws Exception
     * @returnType ServerResponse<HServiceOrderUpdateApply>
     * @throw
     */
    @ResponseBody
    @RequestMapping("/agreeServiceOrderUpdateApply")
    public ServerResponse<HServiceOrderUpdateApply> updateApplyStatusForOk(
            HttpServletRequest request, @RequestParam("model") String applyId) {
        ServerResponse<HServiceOrderUpdateApply> result = null;
        try {
            result = hServiceOrderUpdateApplyService
                    .updateApplyStatus(
                            request,
                            applyId,
                            HConstant.D_HCLV_D_SERVICE_ORDER_UPDATE_ALLPY_STATE_AGREE);
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByErrorMessage(e.getMessage());
        }
        return result;
    }

    /**
     * model:1、申请的id
     *
     * @description 对修改申请的批复, 拒绝修改
     * @methodName updateApplyStatus
     * @param
     * @throws Exception
     * @returnType ServerResponse<HServiceOrderUpdateApply>
     * @throw
     */
    @ResponseBody
    @RequestMapping("/rejectServiceOrderUpdateApply")
    public ServerResponse<HServiceOrderUpdateApply> updateApplyStatusForRefuse(
            HttpServletRequest request, @RequestParam("model") String applyId) {
        ServerResponse<HServiceOrderUpdateApply> result = null;
        try {
            result = hServiceOrderUpdateApplyService
                    .updateApplyStatus(request, applyId,
                            HConstant.D_HCLV_D_SERVICE_ORDER_UPDATE_ALLPY_STATE_NO);
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByErrorMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 业务描述：订单支付 ，订单详情，点击支付，参数：orderNo,List<HServicePayItem>，更新订单支付状态
     *
     * @description TODO
     * @methodName addPay
     * @param
     * @throws Exception
     * @returnType ServerResponse<List < HServicePayItem>>
     * @throw
     */
    @ResponseBody
    @RequestMapping("/addPay")
    public ServerResponse<List<HServicePayItem>> addPay(
            HttpServletRequest request, @RequestParam("model") String model) {
        ServerResponse<List<HServicePayItem>> result = null;
        try {
            result = hServicePayItemService
                    .addPayment(request, model);
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByErrorMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 业务描述：获取当前机构中的额所有的退款记录，并进行分页    查询条件：orgId 后端获取，订单号，退款状态
     * @description TODO
     * @methodName getRebackList
     * @param
     * @returnType ServerResponse<FleaPage < HServicePayRebackItem>>
     * @throw
     */
    @ResponseBody
    @RequestMapping("/getListByOrgId")
    public ServerResponse<FleaPage<HServicePayRebackItem>> getRebackList(
            HttpServletRequest request, FleaPage<HServicePayRebackItem> paging) {
        String orderNo = request.getParameter("orderNo");
        String rebackStatus = request.getParameter("rebackStatus");
        ServerResponse<FleaPage<HServicePayRebackItem>> result = hServicePayRebackItemService.findByOrgIdForPaging(request, paging.getCurPage(), paging.getPageSize(), orderNo, rebackStatus);
        return result;
    }

    /**
     * 业务描述：弹出退款记录详情表，添加退款
     * @description TODO
     * @methodName getRebackList
     * @param
     * @throws Exception
     * @returnType ServerResponse<FleaPage < HServicePayRebackItem>>
     * @throw
     */
    @Deprecated
    @ResponseBody
    @RequestMapping("/rebackMoney")
    public ServerResponse<HServicePayRebackItem> updateReabckAndRebackMoney(
            HttpServletRequest request, @RequestParam("model") String model) {

        ServerResponse<HServicePayRebackItem> result = null;
        try {
            result = hServicePayRebackItemService.updateRebackRecoder(request, model);
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByErrorMessage(e.getMessage());
        }
        return result;
    }
  /**
   * 服务组织订单退款 updateApplyId   rebackRecoder
   * @return
   */
  @ResponseBody
  @RequestMapping("addServiceOrderRebackRecoder")
  public ServerResponse addServiceOrderRebackRecoder(HttpServletRequest request){
    String model = request.getParameter("model");
    try {
      ServerResponse result = hServicePayRebackItemService.addServiceOrderRecbackRecoder(model,request);
      return result;
    } catch (Exception e) {
      return ServerResponse.createByErrorMessage(e.getMessage());
    }
  }
  /**
   * 根据订单号获退款记录
   * @param request
   * @return
   */
  @ResponseBody
  @RequestMapping("getServiceOrderRebackRecoder")
  public ServerResponse getServiceOrderRebackRecoder(HttpServletRequest request) {
    String model = request.getParameter("orderNo");
    ServerResponse<List<HServicePayRebackItem>> result = null;
    try {
      result = hServicePayRebackItemService.findByOrderNo(model);
    } catch (Exception e) {
      return ServerResponse.createByErrorMessage(e.getStackTrace().toString());
    }
    return result;
  }
  /**
   * 根据订单号获取支付记录
   * @param request
   * @return
   */
  @ResponseBody
  @RequestMapping("getPayItmesByOrderNo")
  public ServerResponse getPayItmesByOrderNo(HttpServletRequest request) {
    String model = request.getParameter("model");
    ServerResponse<List<HServicePayItem>> result = null;
    try {
      result = hServicePayItemService.findByOrderNo(model);
    } catch (Exception e) {
      return ServerResponse.createByErrorMessage(e.getStackTrace().toString());
    }
    return result;
  }

    /**
     * 获取我收到的申请列表
     * @param request
     * @param page
     * @return
     */
    @RequestMapping("/listMyRecieveUpdateApply")
    @ResponseBody
    public ServerResponse listMyRecieveUpdateApply(HttpServletRequest request, FleaPage page) {
        String orderNo = request.getParameter("model");
        try {
            ServerResponse result = hServiceOrderUpdateApplyService.findRecievedApply(request, page.getCurPage(), page.getPageSize(), orderNo);
            return result;
        } catch (Exception e) {
            return ServerResponse.createByErrorMessage(e.getMessage());
        }

    }

    /**
     * 获取部门提出的申请列表
     * @param request
     * @param page
     * @return
     */
    @RequestMapping("/listApplerUpdateApply")
    @ResponseBody
    public ServerResponse listApplerUpdateApply(HttpServletRequest request, FleaPage page) {
        String orderNo = request.getParameter("model");

        try {
            ServerResponse result = hServiceOrderUpdateApplyService.findByApplyerId(request, page.getCurPage(), page.getPageSize(), orderNo);
            return result;
        } catch (Exception e) {
            return ServerResponse.createByErrorMessage(e.getMessage());
        }

    }

    @RequestMapping("/getOrderByOApplyId")
    @ResponseBody
    public ServerResponse getOrderByOApplyId(HttpServletRequest request) {
        String appelyId = request.getParameter("model");

        try {
            ServerResponse result = hServiceOrderUpdateApplyService.findByApperId(appelyId);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByErrorMessage(e.getMessage());
        }

    }

  /**
   * 根据班期id获取所有服务订单信息
   * @param request
   * @param paging
   * @return
   */
    @RequestMapping("/getServiceOrderListByTimeTableId")
    @ResponseBody
    public ServerResponse getServiceOrderListByTimeTableId(HttpServletRequest request,FleaPage<HTimeTable> paging){
      String timeTableId = request.getParameter("model");
      try {
        ServerResponse result = hServiceOrderService.findByTimeTableId(timeTableId,request,paging.getCurPage(),paging.getPageSize());
        return  result;
      } catch (Exception e) {
        e.printStackTrace();
        return ServerResponse.createByErrorMessage(e.getMessage());
      }

    }




}
